//
//  NavigationBarButtonItem.m
//  CustomNavigationBar
//
//  Created by zkingsoft on 15/12/29.
//  Copyright (c) 2015年 zkingsoft. All rights reserved.
//

#import "NavigationBarButtonItem.h"

@implementation NavigationBarButtonItem

- (void)dealloc {
    self.target = nil;
    self.selector = nil;
}

/**
 *  初始化item类型
 *
 *  @param itemType
 *
 *  @return
 */
- (id)initWithType:(NavBarButtonItemType)itemType{
    self = [super init];
    if (self) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        self.button  = button;
        self.itemType = itemType;
        button.titleLabel.font  = [UIFont systemFontOfSize:18];
        
        [button setTitleColor:ItemTextNormalColor forState:UIControlStateNormal];//为选中的字体颜色
        [button setTitleColor:ItemTextSelectedColor forState:UIControlStateHighlighted];// 高亮下的字体颜色
        [button setTitleColor:ItemTextSelectedColor forState:UIControlStateSelected];// 选中状态下的字体颜色
        
        button.frame =CGRectMake(0, 0, ItemWidth, ItemHeight);
    }
    return self;
}


// 默认初始化 使用title
+ (id)defauleItemWithTarget:(id)target
                     action:(SEL)action
                      title:(NSString *)title{
    
    NavigationBarButtonItem *item = [[NavigationBarButtonItem alloc] initWithType:NavBarButtonItemTypeDefault];
    item.title = title;
    [item setTarget:target withAction:action];
    return item;
}


// 默认初始化 使用image
+ (id)defauleItemWithTarget:(id)target
                     action:(SEL)action
                      image:(NSString *)image{
    
    NavigationBarButtonItem *item = [[NavigationBarButtonItem alloc] initWithType:NavBarButtonItemTypeDefault];
    item.image = image;
    [item setTarget:target withAction:action];
    return item;
}



// 默认初始化 返回按钮 使用title
+ (id)backItemWithTarget:(id)target
                  action:(SEL)action
                   title:(NSString *)title{
    
    NavigationBarButtonItem *item = [[NavigationBarButtonItem alloc] initWithType:NavBarButtonItemTypeBack];
    item.title = title;
    [item setTarget:target withAction:action];
    return item;
}


// itemtype 的set方法
- (void)setItemType:(NavBarButtonItemType)itemType{
    _itemType = itemType;
    UIImage *image;
    UIImage *image_s;
    switch (itemType) {
        case NavBarButtonItemTypeBack:
        {
            image = [UIImage imageNamed:BackItemImage];
            image_s = [UIImage imageNamed:BackItemSelectedImage];
        }
            break;
        case NavBarButtonItemTypeDefault:
        {
            image = [UIImage imageNamed:ItemImage];
            image_s = [UIImage imageNamed:ItemSelectedImage];
        }
            break;
        default:
            break;
    }
    
    [_button setBackgroundImage:image forState:UIControlStateNormal];
    [_button setBackgroundImage:image_s forState:UIControlStateHighlighted];
    [_button setBackgroundImage:image_s forState:UIControlStateSelected];
    
    [self  titleOffsetWithType];
}


// title的set方法
- (void)setTitle:(NSString *)title
{
    _title = title;
    [_button setTitle:title forState:UIControlStateNormal];
    [_button setTitle:title forState:UIControlStateHighlighted];
    [_button setTitle:title forState:UIControlStateSelected];
    
    [self  titleOffsetWithType];
}


// iamge的set方法
- (void)setImage:(NSString *)image
{
    _image = image;
    UIImage *image_ = [UIImage imageNamed:image];
    [_button setImage:image_  forState:UIControlStateNormal];
    [_button setImage:image_ forState:UIControlStateHighlighted];
    [_button setImage:image_ forState:UIControlStateSelected];
}


// font的set方法
- (void)setFont:(UIFont *)font
{
    _font = font;
    [_button.titleLabel setFont:font];
}


// 默认颜色的set方法
- (void)setNormalColor:(UIColor *)normalColor
{
    _normalColor = normalColor;
    [_button setTitleColor:normalColor forState:UIControlStateNormal];
}


// 选中颜色的set方法
- (void)setSelectedColor:(UIColor *)selectedColor
{
    _selectedColor = selectedColor;
    [_button setTitleColor:selectedColor forState:UIControlStateHighlighted];
    [_button setTitleColor:selectedColor forState:UIControlStateSelected];
}

- (void)setTarget:(id)target withAction:(SEL)action
{
    [_button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
}


// 根据类型然后便宜 item
- (void)titleOffsetWithType
{
    switch (_itemType) {
        case NavBarButtonItemTypeBack:// 当是返回按钮的时候向右偏移5
        {
            [_button setContentEdgeInsets:UIEdgeInsetsMake(0, 5, 0, 0)];
        }
            break;
        case NavBarButtonItemTypeDefault://  其他button item的时候是不偏移的
        {
            [_button setContentEdgeInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
        }
            break;
        default:
            break;
    }
}

- (void)setHeighlightedWhileSwitch:(BOOL)heighlightedWhileSwitch
{
    UIImage *image;
    if (!heighlightedWhileSwitch) {
        switch (_itemType) {
            case NavBarButtonItemTypeBack:
            {
                image = [UIImage imageNamed:BackItemImage];
            }
                break;
            case NavBarButtonItemTypeDefault:
            {
                image = [UIImage imageNamed:ItemImage];
            }
                break;
            default:
                break;
        }
    }else
    {
        switch (_itemType) {
            case NavBarButtonItemTypeBack:
            {
                image = [UIImage imageNamed:BackItemSelectedImage];
            }
                break;
            case NavBarButtonItemTypeDefault:
            {
                image = [UIImage imageNamed:ItemSelectedImage];
                
            }
                break;
            default:
                break;
        }
    }
    [_button setBackgroundImage:image forState:UIControlStateNormal];
    [_button setBackgroundImage:image forState:UIControlStateHighlighted];
    [_button setBackgroundImage:image forState:UIControlStateSelected];
}


@end
